package com.handler;

import com.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//接口/类    注解   配置
@Controller
@RequestMapping(value="handler") //映射
public class SpringMVCHandler {
    @RequestMapping(value="welcome",method=RequestMethod.POST,params= {"name=zs","age!=23","!height"})//映射
    public String  welcome() {
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }


    @RequestMapping(value="welcome2",headers= {"Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Encoding=gzip, deflate"})
    public String  welcome2() {
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }


    @RequestMapping(value="welcome3/**/test")
    public String  welcome3() {
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }
    @RequestMapping(value="welcome4/a?c/test")
    public String  welcome4() {
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }
    @RequestMapping(value="welcome5/{name}")
    public String  welcome5(@PathVariable("name") String name ) {
        System.out.println(name);
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testRest/{id}",method=RequestMethod.POST)
    public String  testPost(@PathVariable("id") Integer id) {
        System.out.println("post：增 " +id);
        //Service层实现 真正的增
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testRest/{id}",method=RequestMethod.GET)
    public String  testGet(@PathVariable("id") Integer id) {
        System.out.println("get：查 " +id);
        //Service层实现 真正的增
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testRest/{id}",method=RequestMethod.DELETE)
    public String  testDelete(@PathVariable("id") Integer id) {
        System.out.println("delete：删 " +id);
        //Service层实现 真正的增
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testRest/{id}",method=RequestMethod.PUT)
    public String  testPut(@PathVariable("id") Integer id) {
        System.out.println("put：改 " +id);
        //Service层实现 真正的增
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testParam")
    public String  testParam(@RequestParam("uname") String name,@RequestParam(value="uage",required=false,defaultValue="23") Integer age) {
//			String name = request.getParameter("uname");

        System.out.println(name);
        System.out.println(age);
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testRequestHeader")
    public String  testRequestHeader(@RequestHeader("Accept-Language")  String al  ) {
        System.out.println( al);
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }


    @RequestMapping(value="testCookieValue")
    public String  testCookieValue(@CookieValue("JSESSIONID") String jsessionId) {
        System.out.println( jsessionId);
        return "success" ;//  views/success.jsp，默认使用了 请求转发的 跳转方式
    }

    @RequestMapping(value="testObjectProperties")
    public String  testObjectProperties(Student student) {//student属性 必须 和 form表单中的属性Name值一致（支持级联属性）
		/*
			String name = 	request.getParameter("name");
		int age= Integer.parseInt(request.getParameter("age")s)	;
		String haddrss = 	request.getParameter("homeaddress");
		String saddress = 	request.getParameter("schooladdress");
		Address address = new Address();
		address.setHomeAddress(haddrss);
		address.setSchoolAddress(saddress);

			Student student = new Student();
			student.setName(name);
			student.setAddress(address);
		*/
        System.out.println(student.getId()+","+student.getName()+","+student.getAddress().getHomeAddress()+","+student.getAddress().getSchoolAddress());
        return "success" ;
    }
    @RequestMapping(value="testServletAPI")
    public String testServletAPI(HttpServletRequest  request,HttpServletResponse response) {
//			request.getParameter("uname") ;
        System.out.println(request);
        return "success" ;
    }




}
